<?php
// +----------------------------------------------------------------------
// |     【FabricSpare】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\SupplierMain as SUP;

class FabricSpare extends Model{
    use SoftDelete;
    protected $deleteTime = false;
    
    protected static $prefix;
     
    public function __construct(array $data = []){
        parent::__construct($data);
        self::$prefix = env('database.prefix'); // 获取表名前缀
    }
    
    /**
     * 供应商一对一关联
     */
    public function supplier(){
        return $this->belongsTo(SUP::class, 'sup_id', 'id')->bind(['sup_name']);
    }
    
    // 获取列表
    public static function getList($where = []){
        $limit = input('get.limit/d', 20);
        
        // 按色号查找
        if ($color_code = input("color_code")) {
            $where[] = ["color_code", "=", $color_code];
        }
        
        // 按渠道编号查找
        if ($sup_code = input("sup_code")) {
            $where[] = ["sup_code", "=", $sup_code];
        }
        
        // 按渠道色号查找
        if ($sup_color = input("sup_color")) {
            $where[] = ["sup_color", "=", $sup_color];
        }
            
        // 按渠道名称查找
        if ($sup_name = input("sup_name")) {
            $where[] = ["sup.sup_name", "like", "%" . $sup_name . "%"];
        }
        
        // 按面料编号查找
        if ($fabric_code = input("fabric_code")) {
            $where[] = ["fabric_code", "=", $fabric_code];
        }
        
        $list = self::alias('sp')
         // 关联渠道商表
         ->join(self::$prefix . 'supplier_main sup', 'sp.sup_id = sup.id')
         // 按条件查询
         ->where($where)
         // 指定字段
         ->field('sp.*, sup.sup_name')
         // 排序
         ->order([
             'sp.id' => 'desc',
         ])
         // 分页
         ->paginate($limit);
        
        return [
            'code' => 0,
            'data' => $list->items(),
            'extend' => [
             'count' => $list->total(),
             'limit' => $limit
            ]
        ];
    }
	
	// 获取列表数据
	public static function listData($where=[],$order="",$n_page=0,$limit=20){
		$data=self::order($order)->where($where);
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();
		
		return $data;
	}
	
	// 获取某个单条记录
	public static function info($id){
		if(!is_array($id)){
			$model=self::with('supplier')->find($id);
		}else{
			$model=self::with('supplier')->where($id)->find();
		}
		return $model;		
	}
	
	/** 获取器 **/
	
}
